home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 131HPDK (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  12.5 KB  |  384 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractAction;
  4. import com.sun.java.swing.CellRendererPane;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.JList;
  7. import com.sun.java.swing.KeyStroke;
  8. import com.sun.java.swing.ListCellRenderer;
  9. import com.sun.java.swing.ListModel;
  10. import com.sun.java.swing.ListSelectionModel;
  11. import com.sun.java.swing.LookAndFeel;
  12. import com.sun.java.swing.UIManager;
  13. import com.sun.java.swing.event.ListDataListener;
  14. import com.sun.java.swing.event.ListSelectionListener;
  15. import com.sun.java.swing.plaf.ComponentUI;
  16. import com.sun.java.swing.plaf.ListUI;
  17. import com.sun.java.swing.plaf.UIResource;
  18. import java.awt.Color;
  19. import java.awt.Component;
  20. import java.awt.Dimension;
  21. import java.awt.Graphics;
  22. import java.awt.Insets;
  23. import java.awt.LayoutManager;
  24. import java.awt.Point;
  25. import java.awt.Rectangle;
  26. import java.beans.PropertyChangeListener;
  27. import java.io.Serializable;
  28.  
  29. public class BasicListUI extends ListUI implements Serializable {
  30.    protected transient boolean hasFocus = false;
  31.    protected ListSelectionListener selectionL;
  32.    protected ListDataListener dataL;
  33.    protected PropertyChangeListener propertyL;
  34.    protected InputListener inputL;
  35.    protected JList list = null;
  36.    protected CellRendererPane rendererPane;
  37.    protected int[] cellHeights = null;
  38.    protected int cellHeight = -1;
  39.    protected int cellWidth = -1;
  40.    protected int updateLayoutStateNeeded = 1;
  41.    protected static final int modelChanged = 1;
  42.    protected static final int selectionModelChanged = 2;
  43.    protected static final int fontChanged = 4;
  44.    protected static final int fixedCellWidthChanged = 8;
  45.    protected static final int fixedCellHeightChanged = 16;
  46.    protected static final int prototypeCellValueChanged = 32;
  47.    protected static final int cellRendererChanged = 64;
  48.  
  49.    protected void addListListeners() {
  50.       this.inputL = this.createInputListener();
  51.       this.list.addMouseListener(this.inputL);
  52.       this.list.addMouseMotionListener(this.inputL);
  53.       this.propertyL = this.createPropertyListener();
  54.       this.list.addPropertyChangeListener(this.propertyL);
  55.       this.list.addFocusListener(this.inputL);
  56.       this.dataL = this.createDataListener();
  57.       ListModel model = this.list.getModel();
  58.       if (model != null) {
  59.          model.addListDataListener(this.dataL);
  60.       }
  61.  
  62.       this.selectionL = this.createSelectionListener();
  63.       ListSelectionModel selectionModel = this.list.getSelectionModel();
  64.       if (selectionModel != null) {
  65.          selectionModel.addListSelectionListener(this.selectionL);
  66.       }
  67.  
  68.    }
  69.  
  70.    protected void configureList() {
  71.       this.list.setLayout((LayoutManager)null);
  72.       LookAndFeel.installBorder(this.list, "List.border");
  73.       LookAndFeel.installColorsAndFont(this.list, "List.background", "List.foreground", "List.font");
  74.       if (this.list.getCellRenderer() == null) {
  75.          this.list.setCellRenderer((ListCellRenderer)UIManager.get("List.cellRenderer"));
  76.       }
  77.  
  78.       Color sbg = this.list.getSelectionBackground();
  79.       if (sbg == null || sbg instanceof UIResource) {
  80.          this.list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
  81.       }
  82.  
  83.       Color sfg = this.list.getSelectionForeground();
  84.       if (sfg == null || sfg instanceof UIResource) {
  85.          this.list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
  86.       }
  87.  
  88.       this.rendererPane = new CellRendererPane();
  89.       this.list.add(this.rendererPane);
  90.    }
  91.  
  92.    protected int convertRowToY(int row) {
  93.       int nrows = this.list.getModel().getSize();
  94.       Insets insets = this.list.getInsets();
  95.       if (row >= 0 && row <= nrows) {
  96.          if (this.cellHeights == null) {
  97.             return insets.top + this.cellHeight * row;
  98.          } else {
  99.             int y = insets.top;
  100.  
  101.             for(int i = 0; i < row; ++i) {
  102.                y += this.cellHeights[i];
  103.             }
  104.  
  105.             return y;
  106.          }
  107.       } else {
  108.          return -1;
  109.       }
  110.    }
  111.  
  112.    protected int convertYToRow(int y0) {
  113.       int nrows = this.list.getModel().getSize();
  114.       Insets insets = this.list.getInsets();
  115.       if (this.cellHeights == null) {
  116.          int row = this.cellHeight == 0 ? 0 : (y0 - insets.top) / this.cellHeight;
  117.          return row >= 0 && row < nrows ? row : -1;
  118.       } else {
  119.          int y = insets.top;
  120.          int row = 0;
  121.  
  122.          for(int i = 0; i < nrows; ++i) {
  123.             if (y0 >= y && y0 < y + this.cellHeights[i]) {
  124.                return row;
  125.             }
  126.  
  127.             y += this.cellHeights[i];
  128.             ++row;
  129.          }
  130.  
  131.          return -1;
  132.       }
  133.    }
  134.  
  135.    protected ListDataListener createDataListener() {
  136.       return new DataListener(this);
  137.    }
  138.  
  139.    protected InputListener createInputListener() {
  140.       return new InputListener(this);
  141.    }
  142.  
  143.    protected PropertyChangeListener createPropertyListener() {
  144.       return new PropertyListener(this);
  145.    }
  146.  
  147.    protected ListSelectionListener createSelectionListener() {
  148.       return new SelectionListener(this);
  149.    }
  150.  
  151.    public static ComponentUI createUI(JComponent list) {
  152.       return new BasicListUI();
  153.    }
  154.  
  155.    public Rectangle getCellBounds(JList list, int index1, int index2) {
  156.       this.maybeUpdateLayoutState();
  157.       int minIndex = Math.min(index1, index2);
  158.       int maxIndex = Math.max(index1, index2);
  159.       int minY = this.convertRowToY(minIndex);
  160.       int maxY = this.convertRowToY(maxIndex);
  161.       if (minY != -1 && maxY != -1) {
  162.          Insets insets = ((JComponent)list).getInsets();
  163.          int x = insets.left;
  164.          int w = ((JComponent)list).getWidth() - (insets.left + insets.right);
  165.          int h = maxY + this.getRowHeight(maxIndex) - minY;
  166.          return new Rectangle(x, minY, w, h);
  167.       } else {
  168.          return null;
  169.       }
  170.    }
  171.  
  172.    public Dimension getMaximumSize(JComponent c) {
  173.       return this.getPreferredSize(c);
  174.    }
  175.  
  176.    public Dimension getMinimumSize(JComponent c) {
  177.       return this.getPreferredSize(c);
  178.    }
  179.  
  180.    public Dimension getPreferredSize(JComponent c) {
  181.       this.maybeUpdateLayoutState();
  182.       int lastRow = this.list.getModel().getSize() - 1;
  183.       if (lastRow < 0) {
  184.          return new Dimension(0, 0);
  185.       } else {
  186.          Insets insets = this.list.getInsets();
  187.          int width = this.cellWidth + insets.left + insets.right;
  188.          int height = this.convertRowToY(lastRow) + this.getRowHeight(lastRow) + insets.bottom;
  189.          return new Dimension(width, height);
  190.       }
  191.    }
  192.  
  193.    protected int getRowHeight(int row) {
  194.       if (row >= 0 && row < this.list.getModel().getSize()) {
  195.          return this.cellHeights == null ? this.cellHeight : (row < this.cellHeights.length ? this.cellHeights[row] : -1);
  196.       } else {
  197.          return -1;
  198.       }
  199.    }
  200.  
  201.    public Point indexToLocation(JList list, int index) {
  202.       this.maybeUpdateLayoutState();
  203.       return new Point(0, this.convertRowToY(index));
  204.    }
  205.  
  206.    public void installUI(JComponent list) {
  207.       this.list = (JList)list;
  208.       this.configureList();
  209.       this.addListListeners();
  210.       this.registerKeyboardActions();
  211.    }
  212.  
  213.    public int locationToIndex(JList list, Point location) {
  214.       this.maybeUpdateLayoutState();
  215.       return this.convertYToRow(location.y);
  216.    }
  217.  
  218.    protected void maybeUpdateLayoutState() {
  219.       if (this.updateLayoutStateNeeded != 0) {
  220.          this.updateLayoutState();
  221.          this.updateLayoutStateNeeded = 0;
  222.       }
  223.  
  224.    }
  225.  
  226.    public void paint(Graphics g, JComponent c) {
  227.       this.maybeUpdateLayoutState();
  228.       ListCellRenderer renderer = this.list.getCellRenderer();
  229.       ListModel dataModel = this.list.getModel();
  230.       ListSelectionModel selModel = this.list.getSelectionModel();
  231.       if (renderer != null && dataModel.getSize() != 0) {
  232.          Rectangle paintBounds = g.getClipBounds();
  233.          int firstPaintRow = this.convertYToRow(paintBounds.y);
  234.          int lastPaintRow = this.convertYToRow(paintBounds.y + paintBounds.height - 1);
  235.          if (firstPaintRow == -1) {
  236.             firstPaintRow = 0;
  237.          }
  238.  
  239.          if (lastPaintRow == -1) {
  240.             lastPaintRow = dataModel.getSize() - 1;
  241.          }
  242.  
  243.          Rectangle rowBounds = this.getCellBounds(this.list, firstPaintRow, firstPaintRow);
  244.          if (rowBounds != null) {
  245.             int leadIndex = this.list.getLeadSelectionIndex();
  246.  
  247.             for(int row = firstPaintRow; row <= lastPaintRow; ++row) {
  248.                rowBounds.height = this.getRowHeight(row);
  249.                g.setClip(rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height);
  250.                g.clipRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
  251.                this.paintCell(g, row, rowBounds, renderer, dataModel, selModel, leadIndex);
  252.                rowBounds.y += rowBounds.height;
  253.             }
  254.  
  255.          }
  256.       }
  257.    }
  258.  
  259.    protected void paintCell(Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, ListModel dataModel, ListSelectionModel selModel, int leadIndex) {
  260.       Object value = dataModel.getElementAt(row);
  261.       boolean cellHasFocus = this.hasFocus && row == leadIndex;
  262.       boolean isSelected = selModel.isSelectedIndex(row);
  263.       Component rendererComponent = cellRenderer.getListCellRendererComponent(this.list, value, row, isSelected, cellHasFocus);
  264.       int cx = rowBounds.x;
  265.       int cy = rowBounds.y;
  266.       int cw = rowBounds.width;
  267.       int ch = rowBounds.height;
  268.       this.rendererPane.paintComponent(g, rendererComponent, this.list, cx, cy, cw, ch, true);
  269.    }
  270.  
  271.    protected void registerKeyboardActions() {
  272.       AbstractAction action = new 1(this, "SelectPreviousRow");
  273.       KeyStroke key = KeyStroke.getKeyStroke(38, 0);
  274.       this.list.registerKeyboardAction(action, key, 0);
  275.       action = new 2(this, "SelectNextRow");
  276.       key = KeyStroke.getKeyStroke(40, 0);
  277.       this.list.registerKeyboardAction(action, key, 0);
  278.    }
  279.  
  280.    protected void removeListListeners() {
  281.       this.list.removeMouseListener(this.inputL);
  282.       this.list.removeMouseMotionListener(this.inputL);
  283.       this.list.removePropertyChangeListener(this.propertyL);
  284.       this.list.removeFocusListener(this.inputL);
  285.       ListModel model = this.list.getModel();
  286.       if (model != null) {
  287.          model.removeListDataListener(this.dataL);
  288.       }
  289.  
  290.       ListSelectionModel selectionModel = this.list.getSelectionModel();
  291.       if (selectionModel != null) {
  292.          selectionModel.removeListSelectionListener(this.selectionL);
  293.       }
  294.  
  295.    }
  296.  
  297.    protected void selectNextIndex() {
  298.       int s = this.list.getSelectedIndex();
  299.       if (s + 1 < this.list.getModel().getSize()) {
  300.          ++s;
  301.          this.list.setSelectedIndex(s);
  302.          this.list.ensureIndexIsVisible(s);
  303.       }
  304.  
  305.    }
  306.  
  307.    protected void selectPreviousIndex() {
  308.       int s = this.list.getSelectedIndex();
  309.       if (s > 0) {
  310.          --s;
  311.          this.list.setSelectedIndex(s);
  312.          this.list.ensureIndexIsVisible(s);
  313.       }
  314.  
  315.    }
  316.  
  317.    protected void unconfigureList() {
  318.       this.list.remove(this.rendererPane);
  319.       this.rendererPane = null;
  320.       if (this.list.getCellRenderer() instanceof UIResource) {
  321.          this.list.setCellRenderer((ListCellRenderer)null);
  322.       }
  323.  
  324.    }
  325.  
  326.    public void uninstallUI(JComponent c) {
  327.       this.removeListListeners();
  328.       this.unregisterKeyboardActions();
  329.       this.unconfigureList();
  330.       this.cellWidth = this.cellHeight = -1;
  331.       this.cellHeights = null;
  332.       this.list = null;
  333.    }
  334.  
  335.    protected void unregisterKeyboardActions() {
  336.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(38, 0));
  337.       this.list.unregisterKeyboardAction(KeyStroke.getKeyStroke(40, 0));
  338.    }
  339.  
  340.    protected void updateLayoutState() {
  341.       int fixedCellHeight = this.list.getFixedCellHeight();
  342.       int fixedCellWidth = this.list.getFixedCellWidth();
  343.       this.cellWidth = fixedCellWidth != -1 ? fixedCellWidth : -1;
  344.       if (fixedCellHeight != -1) {
  345.          this.cellHeight = fixedCellHeight;
  346.          this.cellHeights = null;
  347.       } else {
  348.          this.cellHeight = -1;
  349.          this.cellHeights = new int[this.list.getModel().getSize()];
  350.       }
  351.  
  352.       if (fixedCellWidth == -1 || fixedCellHeight == -1) {
  353.          ListModel dataModel = this.list.getModel();
  354.          int dataModelSize = dataModel.getSize();
  355.          ListCellRenderer renderer = this.list.getCellRenderer();
  356.          if (renderer != null) {
  357.             for(int index = 0; index < dataModelSize; ++index) {
  358.                Object value = dataModel.getElementAt(index);
  359.                Component c = renderer.getListCellRendererComponent(this.list, value, index, false, false);
  360.                this.rendererPane.add(c);
  361.                Dimension cellSize = c.getPreferredSize();
  362.                if (fixedCellWidth == -1) {
  363.                   this.cellWidth = Math.max(cellSize.width, this.cellWidth);
  364.                }
  365.  
  366.                if (fixedCellHeight == -1) {
  367.                   this.cellHeights[index] = cellSize.height;
  368.                }
  369.             }
  370.          } else {
  371.             if (this.cellWidth == -1) {
  372.                this.cellWidth = 0;
  373.             }
  374.  
  375.             for(int index = 0; index < dataModelSize; ++index) {
  376.                this.cellHeights[index] = 0;
  377.             }
  378.          }
  379.       }
  380.  
  381.       this.list.invalidate();
  382.    }
  383. }
  384.